package com.ctshk.rpc.visa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import com.ctshk.rpc.visa.dto.visa.VisaOrderRefundDTO;
import com.ctshk.rpc.visa.entity.VisaOrderCollection;
import com.ctshk.rpc.visa.entity.VisaOrderRefund;
import com.ctshk.rpc.visa.mapper.VisaOrderCollectionMapper;
import com.ctshk.rpc.visa.mapper.VisaOrderMapper;
import com.ctshk.rpc.visa.mapper.VisaOrderRefundMapper;
import com.ctshk.rpc.visa.req.visa.VisaOrderRefundDetailReq;
import com.ctshk.rpc.visa.req.visa.VisaOrderRefundReq;
import com.ctshk.rpc.visa.service.IVisaOrderRefundService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 签证退款记录 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-04
 */
@DubboService
public class VisaOrderRefundServiceImpl extends ServiceImpl<VisaOrderRefundMapper, VisaOrderRefund> implements IVisaOrderRefundService {
    @Autowired
    private VisaOrderMapper visaOrderMapper;
    @Autowired
    private VisaOrderRefundMapper visaOrderRefundMapper;
    @Autowired
    private VisaOrderCollectionMapper visaOrderCollectionMapper;
    @DubboReference
    private ISysUserService iSysUserService;

    @Override
    public Result<List<VisaOrderRefundDTO>> list(VisaOrderRefundReq visaOrderRefundReq) {
        List<VisaOrderRefundDTO> visaOrderRefundDTOList = new ArrayList<>();
        QueryWrapper<VisaOrderRefund> visaOrderRefundQueryWrapper = new QueryWrapper<>();
        visaOrderRefundQueryWrapper.eq("order_id", visaOrderRefundReq.getOrderId());
        visaOrderRefundQueryWrapper.eq("is_deleted", 0);
        List<VisaOrderRefund> visaOrderRefundList = visaOrderRefundMapper.selectList(visaOrderRefundQueryWrapper);
        if (visaOrderRefundList != null && visaOrderRefundList.size() >= 1) {
            for (VisaOrderRefund visaOrderRefund : visaOrderRefundList) {
                VisaOrderRefundDTO visaOrderRefundDTO = EntityUtil.copy(visaOrderRefund, VisaOrderRefundDTO.class);
                QueryWrapper<VisaOrderCollection> visaOrderCollectionQueryWrapper = new QueryWrapper<>();
                visaOrderCollectionQueryWrapper.eq("order_id", visaOrderRefundReq.getOrderId());
                List<VisaOrderCollection> visaOrderCollectionList = visaOrderCollectionMapper.selectList(visaOrderCollectionQueryWrapper);
                if (visaOrderCollectionList != null && visaOrderCollectionList.size() >= 0) {
                    VisaOrderCollection visaOrderCollection = visaOrderCollectionList.get(0);
                    //todo   上线去掉
                    UserDTO userDTO = null;
                    UserDTO applicationNameDTO = null;
                    try {
                        userDTO = iSysUserService.loadUserByParam(visaOrderCollection.getCreateId(), 1);
                        //收款人
                        visaOrderRefundDTO.setCollectionName(userDTO.getUsername());
                        applicationNameDTO = iSysUserService.loadUserByParam(visaOrderRefund.getCreateId(), 1);
                        //申请人
                        visaOrderRefundDTO.setApplicationName(applicationNameDTO.getUsername());
                    } catch (Exception e) {
                        throw new BusinessException(SystemError.SYS_502);
                    }
                    visaOrderRefundDTOList.add(visaOrderRefundDTO);
                }
            }
        }
        return Result.success(visaOrderRefundDTOList);
    }

    @Override
    public Result<Long> rufund(VisaOrderRefundDetailReq visaOrderRefundDetailReq) {
        VisaOrderRefund visaOrderRefund = visaOrderRefundMapper.selectById(visaOrderRefundDetailReq.getId());
        visaOrderRefund.setStatus(2);
        visaOrderRefundMapper.updateById(visaOrderRefund);
        return Result.success(visaOrderRefund.getId());
    }

    @Override
    public Result<Long> delete(VisaOrderRefundDetailReq visaOrderRefundDetailReq) {
        VisaOrderRefund visaOrderRefund = visaOrderRefundMapper.selectById(visaOrderRefundDetailReq.getId());
        visaOrderRefund.setIsDeleted(1);
        visaOrderRefundMapper.updateById(visaOrderRefund);
        return Result.success(visaOrderRefund.getId());
    }
}
